package cn.hxzy.controller;

import cn.hxzy.domain.Video;
import cn.hxzy.response.R;
import cn.hxzy.response.StatusEnum;
import cn.hxzy.service.VideoService;
import cn.hxzy.util.AliOssUtil;
import com.aliyun.oss.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 *  视频控制器
 */
@RestController
@RequestMapping("/videos")
public class VideoController {
    @Autowired
    private VideoService videoService;

    /**
     * 上传教学视频
     * @param file
     * @return
     */
    @PostMapping("/uploadVideo")
    public R uploadVideo(@RequestParam("file") MultipartFile file) throws Exception {
        //原始的文件名
        String originalFilename = file.getOriginalFilename();
        //新命名文件名称
        String url = AliOssUtil.uploadFile(originalFilename, file.getInputStream());
        if (StringUtils.isNullOrEmpty(url)) {
            return R.error(StatusEnum.PARAM_INVALID);
        }
        Video video = new Video();
        video.setVideoPath(url);
        boolean save = videoService.save(video);
        return R.success(save);
    }
    /**
     * 根据小节id获取教学视频列表
     * @param id
     * @return
     */
    @GetMapping("/getVideoListByTeacherId/{id}")
    public R<List<Video>> getVideoListBySectionId(@PathVariable Long id) {
        List<Video> videos = videoService.getVideoListBySectionId(id);
        return R.success(videos);
    }
    /**
     * 删除教学视频
     * @param id
     * @return
     */
    @DeleteMapping("/deleteVideo/{id}")
    public R deleteVideo(@PathVariable Long id) {
        Boolean b = videoService.deleteVideo(id);
        if (b){
            return R.success("删除成功！");
        }
        return R.error(400,"删除失败！");
    }
    /**
     * 批量删除教学视频
     * @param ids
     * @return
     */
    @DeleteMapping("/deleteVideoBatch")
    public R deleteVideoBatch(@RequestBody Long[] ids) {
        Boolean b = videoService.deleteVideoBatch(ids);
        if (b){
            return R.success("删除成功！");
        }
        return R.error(400,"删除失败！");
    }
}
